package com.rtx.edi.platform.rule.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.rule.bean.WhseidRule;
import com.rtx.edi.platform.rule.service.WhseidRuleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@SuppressWarnings("all")
@Api(tags = {"仓库id确定规则"})
@RestController
@RequestMapping("/v1/whseidRule")
public class WhseidRuleController {
	
	@Autowired
	WhseidRuleService whseidRuleService;
	
	@Autowired
	ApplicationContext context;
	
    
    /**
     * 获取仓库id确定规则
     * @return
     */
    @ApiOperation(value = "分页获取仓库id确定规则", notes = "分页获取仓库id确定规则")
    @PostMapping("selectWhseidRuleRulePageList")
    public EdiResponse selectTargetFieldConvertRulePageList(@RequestBody WhseidRule dto){
        Page<WhseidRule> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
        whseidRuleService.selectList(pageParam,dto);
        return EdiResponse.success().data("page",pageParam);
    }

    /**
     * 仓库id确定规则
     * @return
     */
    @ApiOperation(value = "根据ID查询-仓库id确定规则", notes = "根据ID查询-仓库id确定规则")
    @PostMapping("selectWhseidRuleRuleById")
    public EdiResponse selectPlTargetFieldConvertRuleById(@RequestBody WhseidRule dto){
        return EdiResponse.success().data("item",whseidRuleService.getById(dto.getId()));
    }

    /**
     * 新增仓库id确定规则
     * @return
     */
    @ApiOperation(value = "新增-仓库id确定规则", notes = "新增-仓库id确定规则")
    @PostMapping("addWhseidRule")
    public EdiResponse addWhseidRule(@RequestBody WhseidRule dto){
    	try {
    		dto.setId(null);
    		whseidRuleService.save(dto);
            return EdiResponse.success();
		} catch (Exception e) {
			return EdiResponse.failed().returnMgr(e.getMessage());
		}
        
    }

    /**
     * 修改仓库id确定规则
     * @return
     */
    @ApiOperation(value = "修改-仓库id确定规则", notes = "修改-仓库id确定规则")
    @PostMapping("editWhseidRule")
    public EdiResponse editWhseidRule(@RequestBody WhseidRule dto){
    	try {
    		whseidRuleService.updateById(dto);
            return EdiResponse.success();
		} catch (Exception e) {
			return EdiResponse.failed().returnMgr(e.getMessage());
		}
    	
    }

    /**
     * 批量删除目标字段转换规则
     * @return
     */
    @ApiOperation(value = "批量删除-仓库id确定规则", notes = "批量删除-仓库id确定规则")
    @PostMapping("deleteWhseidRule")
    public EdiResponse deleteTargetFieldConvertRuleByIds(@RequestBody List<WhseidRule> list){
        List<Long> ids = list.stream().map(p -> p.getId()).distinct().collect(Collectors.toList());
        return  whseidRuleService.removeBatchByIds(ids) ? EdiResponse.success() : EdiResponse.failed();
    }
}
